Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bucket.Put: copy key before seek #637

Merged
merged 3 commits into from
Dec 13, 2023
Merged

Conversation

fuweid
Copy link
Member

@fuweid fuweid commented Dec 12, 2023

Application might change key value after seeking and before real put.
This unexpected behaviour could corrupt database. When users file issue,
maintainers doesn't know application behaviour. It could be caused by
data race. This patch is to prevent such case and save maintainers' time.

REF: #72

@fuweid fuweid force-pushed the copy-key-before-seek branch from 6c28521 to 1d491a7 Compare December 12, 2023 14:08
@ahrtr ahrtr added this to the v1.4.0 milestone Dec 12, 2023
tests/failpoint/db_failpoint_test.go Outdated Show resolved Hide resolved
tests/robustness/issue72_test.go Outdated Show resolved Hide resolved
tests/robustness/issue72_test.go Outdated Show resolved Hide resolved
@fuweid fuweid force-pushed the copy-key-before-seek branch from 1d491a7 to 215e351 Compare December 13, 2023 02:53
Comment on lines 161 to 163
// When application inserts key/value, that key can be changed by application
// during insertion. bbolt should copy key before seeking and updating. Otherwise,
// some branch node could point to freed page.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't necessarily cause elements to point freed pages. In this test case, if you change the key to a larger key e.g. 200, the case will pass.

Suggested change
// When application inserts key/value, that key can be changed by application
// during insertion. bbolt should copy key before seeking and updating. Otherwise,
// some branch node could point to freed page.
// When bbolt is processing a `Put` invocation, the key might be concurrently
// updated by the application which calls the `Put` API (although it shouldn't).
// It might lead to a situation that bbolt use an old key to find a proper
// position to insert the key/value pair, but actually inserts a new key.
// Eventually it might break the rule that all keys should be sorted. In a
// worse case, it might cause page elements to point to already freed pages.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comment. Updated

Application might change key value after seeking and before real put.
This unexpected behaviour could corrupt database. When users file issue,
maintainers doesn't know application behaviour. It could be caused by
data race. This patch is to prevent such case and save maintainers' time.

Signed-off-by: Wei Fu <[email protected]>
@fuweid fuweid force-pushed the copy-key-before-seek branch from 215e351 to a05ec68 Compare December 13, 2023 12:58
Copy link
Member

@ahrtr ahrtr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Thanks @fuweid

Please backport the PR to release-1.3 as well.

@ahrtr ahrtr merged commit cd4a757 into etcd-io:main Dec 13, 2023
15 checks passed
fuweid added a commit to fuweid/bbolt that referenced this pull request Dec 17, 2023
fuweid added a commit to fuweid/bbolt that referenced this pull request Dec 17, 2023
fuweid added a commit to fuweid/bbolt that referenced this pull request Dec 18, 2023
It's follow-up of etcd-io#637.

Signed-off-by: Wei Fu <[email protected]>
(cherry picked from commit 62d8026)
Signed-off-by: Wei Fu <[email protected]>
@fuweid fuweid deleted the copy-key-before-seek branch December 18, 2023 15:42
fuweid added a commit to fuweid/bbolt that referenced this pull request Dec 18, 2023
- [bucket: allow to allocate key on stack in Put()](etcd-io#550)
- [bucket.Put: copy key before seek](etcd-io#637)
- [copy key before comparing during CreateBucket or CreateBucketIfNotExists](etcd-io#641)

Signed-off-by: Wei Fu <[email protected]>
fuweid added a commit to fuweid/bbolt that referenced this pull request Dec 18, 2023
- [bucket: allow to allocate key on stack in Put()](etcd-io#550)
- [bucket.Put: copy key before seek](etcd-io#637)
- [copy key before comparing during CreateBucket or CreateBucketIfNotExists](etcd-io#641)

Signed-off-by: Wei Fu <[email protected]>
@ahrtr ahrtr removed this from the v1.4.0 milestone Jan 12, 2024
ishan16696 pushed a commit to ishan16696/bbolt that referenced this pull request Feb 25, 2024
ishan16696 pushed a commit to ishan16696/bbolt that referenced this pull request Feb 25, 2024
ishan16696 pushed a commit to ishan16696/bbolt that referenced this pull request Feb 25, 2024
- [bucket: allow to allocate key on stack in Put()](etcd-io#550)
- [bucket.Put: copy key before seek](etcd-io#637)
- [copy key before comparing during CreateBucket or CreateBucketIfNotExists](etcd-io#641)

Signed-off-by: Wei Fu <[email protected]>
passiondev2024 added a commit to passiondev2024/bbolt that referenced this pull request Mar 20, 2024
- [bucket: allow to allocate key on stack in Put()](etcd-io/bbolt#550)
- [bucket.Put: copy key before seek](etcd-io/bbolt#637)
- [copy key before comparing during CreateBucket or CreateBucketIfNotExists](etcd-io/bbolt#641)

Signed-off-by: Wei Fu <[email protected]>
samuelbartels20 pushed a commit to samuelbartels20/bbolt that referenced this pull request Nov 10, 2024
It's follow-up of etcd-io#637.

Signed-off-by: Wei Fu <[email protected]>
Signed-off-by: samuelbartels20 <[email protected]>
samuelbartels20 pushed a commit to samuelbartels20/bbolt that referenced this pull request Nov 10, 2024
It's follow-up of etcd-io#637.

Signed-off-by: Wei Fu <[email protected]>
Signed-off-by: samuelbartels20 <[email protected]>
samuelbartels20 pushed a commit to samuelbartels20/bbolt that referenced this pull request Nov 10, 2024
- [bucket: allow to allocate key on stack in Put()](etcd-io#550)
- [bucket.Put: copy key before seek](etcd-io#637)
- [copy key before comparing during CreateBucket or CreateBucketIfNotExists](etcd-io#641)

Signed-off-by: Wei Fu <[email protected]>
Signed-off-by: samuelbartels20 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants